package com.gitee.sample.mvp.droidmvp.login;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.gitee.sample.mvp.droidmvp.R;
import com.gitee.sample.mvp.droidmvp.main.MainActivity;

public class LoginFragment extends Fragment implements View.OnClickListener, LoginContract.View {

    private EditText mUserEmailView;
    private EditText mPasswordView;
    private View mProgressView;
    private View mLoginFormView;

    private LoginContract.Presenter mPresenter;

    private Button mSignButton, mResetButton;

    public static LoginFragment newInstance(String name) {
        Bundle args = new Bundle();
        args.putString("fragment_name", name);
        LoginFragment fragment = new LoginFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_login, container, false);

        initView(root);
        return root;
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.start();
    }

    private void initView(View view) {
        mUserEmailView = view.findViewById(R.id.email);
        mPasswordView = view.findViewById(R.id.password);

        mLoginFormView = view.findViewById(R.id.login_form);
        mProgressView = view.findViewById(R.id.login_progress);

        mSignButton = view.findViewById(R.id.email_sign_in_button);
        mResetButton = view.findViewById(R.id.reset_sign_in_button);

        mSignButton.setOnClickListener(this);
        mResetButton.setOnClickListener(this);
    }

    @Override
    public String getUserEmail() {
        return mUserEmailView.getText().toString();
    }

    @Override
    public String getPasword() {
        return mPasswordView.getText().toString();
    }

    @Override
    public boolean isEmailValid(String email) {
        return email.contains("@");
    }

    @Override
    public boolean isPasswordValid(String password) {
        return password.length() > 4;
    }

    /**
     * Shows the progress UI and hides the login form.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
    @Override
    public void showLoginProgress(final boolean show) {
        // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
        // for very easy animations. If available, use these APIs to fade-in
        // the progress spinner.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
            int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);

            mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
            mLoginFormView.animate().setDuration(shortAnimTime).alpha(
                    show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
                }
            });

            mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
            mProgressView.animate().setDuration(shortAnimTime).alpha(
                    show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
                }
            });
        } else {
            // The ViewPropertyAnimator APIs are not available, so simply show
            // and hide the relevant UI components.
            mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
            mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
        }
    }

    @Override
    public boolean setEmailError(String error) {
        mUserEmailView.setError(error);
        if (!TextUtils.isEmpty(error)) {
            mUserEmailView.requestFocus();
        }
        return true;
    }

    @Override
    public boolean setPasswordError(String error) {
        mPasswordView.setError(error);
        if (!TextUtils.isEmpty(error)) {
            mPasswordView.requestFocus();
        }
        return true;
    }

    @Override
    public void resetEditView() {
        mUserEmailView.setText("");
        mPasswordView.setText("");
    }

    @Override
    public void toMainActivity() {
        Toast.makeText(getActivity(), "Sign in Success!", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(getActivity(), MainActivity.class);
        startActivity(intent);
    }

    @Override
    public void showFailedError(String message) {
        Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void setPresenter(LoginContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.email_sign_in_button:
                mPresenter.login();
                break;
            case R.id.reset_sign_in_button:
                mPresenter.reset();
                break;
        }
    }
}
